﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace MShop.Core.Domain
{
    [System.ComponentModel.DataAnnotations.MetadataType(typeof(ItemProp_Validation))]
    public partial class ItemProp : INode<ItemProp>
    {
        public ItemProp()
        {
            this.Status = 1;
            this.Sort = 1;
            this.IsAllowAlias = true;

        }


        public string TableRowClass
        {
            get
            {
                if (this.ParentId.HasValue)
                {
                    return string.Concat("child-of-node-", this.ItemProp2.NodeId);
                }
                else
                    return null;
            }
        }

        public string TableRowId
        {
            get
            {
                return string.Concat("node-", NodeId);
            }
        }

        public bool IsRoot
        {
            get { return !this.ParentId.HasValue; }
        }

        public int Depth
        {
            get
            {
                int i = 0;
                var p = this.ItemProp2;
                while (p != null)
                {
                    p = p.ItemProp2;
                    i++;
                }
                return i;
            }
        }

        public int NodeId { get; set; }



        public bool HasChild
        {
            get
            {
                return this.ItemProp1.Count > 0;
            }
        }        

        public ItemProp Parent
        {
            get { return this.ItemProp2; }
        }

        int? INode.ParentId
        {
            get { return ParentId; }
        }

        public object Key
        {
            get { return this.Id; }
        }

        public IEnumerable<ItemProp> ChildNodes
        {
            get
            {
                return this.ItemProp1;
            }
        }

        public string PropValuesToString()
        {
            if (this.ItemPropType == Core.ItemPropType.Input)
            {
                return string.Empty;
            }
            else
            {
                int count=this.ItemPropValue.Select(x => x.PropValue).ToList().Count;
                if (count > 0)
                {
                    return string.Join(",", this.ItemPropValue.Select(x => x.PropValue).ToList());
                }
            }
            return string.Empty; ;
        }

        public string PropValues
        {
            get;
            set;
        }

        public string Field
        {
            get
            {
                return string.Concat("Field" + this.Id);
            }
        }

        public ItemPropType ItemPropType
        {
            get
            {
                return (ItemPropType)Enum.Parse(typeof(ItemPropType), this.PropType);
            }
        }

        public string ItemPropTypeToString()
        {
            object obj = Enum.Parse(typeof(ItemPropType), this.PropType);

                  var attr= typeof(ItemPropType).GetField(obj.ToString()).GetCustomAttributes(typeof(DescriptionAttribute),false).OfType<DescriptionAttribute>().FirstOrDefault();
                  if(attr!=null)
                  {
                      return attr.Description;
                  }
                return this.ItemPropType.ToString();  
           
        }

        public string ItemPropToString()
        {
            
                if (this.IsKeyProp)
                {
                    return "关键属性";
                }
                if (this.IsSaleProp)
                {
                    return "销售属性";
                }
                if (this.IsItemProp)
                {
                    return "非关键属性";
                }
                return string.Empty;
           
        }

        public string GetPropValue(string propValue)
        {
            if (this.IsInputProp)
            {
                return propValue;
            }
            if (this.IsEnumProp)
            {
                int id=int.Parse(propValue);
                return this.ItemPropValue.FirstOrDefault(x => x.Id == id).PropValue;
            }
            return string.Empty;
        }


        public IEnumerable<System.Web.Mvc.SelectListItem> Props
        {
            get
            {
                List<SelectListItem> list = new List<SelectListItem>(){
                      new  SelectListItem(){ Text="关键属性", Value="1"},
                       new  SelectListItem(){ Text="非关键属性", Value="2"},
                        new  SelectListItem(){ Text="销售属性", Value="3"}
                };
                if (this.IsKeyProp)
                {
                    list[0].Selected = true;
                }
                if (this.IsSaleProp)
                {
                    list[2].Selected = true;
                }
                if (this.IsItemProp)
                {
                    list[1].Selected = true;
                }
                return list;
            }       
        }
        
    }

    internal class ItemProp_Validation
    {

        [DisplayName("父节点")]
        public int? ParentId { get; set; }

        [Required(ErrorMessage = "名称不能为空")]
        [DisplayName("名称")]
        public string Name { get; set; }

        [Foolproof.RequiredIf("PropType", 2, ErrorMessage = "属性值不能为空")]
        [DataType(DataType.MultilineText)]
        [DisplayName("属性值")]       
        public string PropValues
        {
            get;
            set;
        }

        [DisplayName("属性类型")]
        public string PropType { get; set; }

        [DisplayName("销售属性")]
        public bool IsSaleProp { get; set; }

        [DisplayName("多选")]
        public bool Multi { get; set; }

        
        [DisplayName("必填")]
        public bool Must { get; set; }

        [DisplayName("是否颜色")]
        public bool IsColorProp { get; set; }

        [DisplayName("属性类型")]
        public IEnumerable<System.Web.Mvc.SelectListItem> Props { get; private set; }
    }
}